/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mz2d.graphics.sprite;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

/**
 *
 * @author Laptop
 */
public class Sprite {

    /**
     * 
     */
    private Image image;
    /**
     *
     */
    private int width;
    /**
     *
     */
    private int height;

    public Sprite(Image image) {
        if (image == null) {
            return; //ADD EXCEPTION
        }
        width = image.getWidth(null);
        height = image.getHeight(null);
        /** Default transparency **/
        ImageFilter filter = new RGBImageFilter() {
            // the color we are looking for... Alpha bits are set to opaque

            public int markerRGB = Color.black.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                } else {
                    // nothing to do
                    return rgb;
                }
            }
        };

        ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
        this.image = Toolkit.getDefaultToolkit().createImage(ip);
    }

    public Sprite(Image image, final Color transparency) {
        if (image == null) {
            return; //ADD EXCEPTION
        }
        width = image.getWidth(null);
        height = image.getHeight(null);
        /** Default transparency **/
        if (transparency != null) {
            ImageFilter filter = new RGBImageFilter() {
                // the color we are looking for... Alpha bits are set to opaque

                public int markerRGB = transparency.getRGB() | 0xFF000000;

                public final int filterRGB(int x, int y, int rgb) {
                    if ((rgb | 0xFF000000) == markerRGB) {
                        // Mark the alpha bits as zero - transparent
                        return 0x00FFFFFF & rgb;
                    } else {
                        // nothing to do
                        return rgb;
                    }
                }
            };

            ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
            this.image = Toolkit.getDefaultToolkit().createImage(ip);
        } else {
            this.image = image;
        }
    }

    /**
     * 
     * @return
     */
    public Image getImage() {
        return image;
    }
}
